<?php
class TicketsController extends AppController {

	var $name = 'Tickets';
	var $scaffold = 'admin';
	var $uses = array('Event','Ticket', 'Seat', 'Order', 'User');
	var $components = array('Auth', 'Session');
    var $helpers = array('Html','Form','PaypalIpn.Paypal');
    
    function beforeFilter() {
        $this->Auth->allow('find', 'delivery', 'login', 'register', 'user');
		}
	
	function find() {
		
		//Fail if no POST Data
		if (empty($this->data)) {
			$this->Session->setFlash('Unable to find tickets, please select an event.');
			$this->redirect('/events/');
			}
		
		//Post Data
		$post = $this->data;
		$this->set('data', $post);
		//Event Data
		$this->set('event', $this->Event->find($post['ticket']['event_id']));
		
		//Find Section Info
		$conditions = array('ticket.event_id' => $post['ticket']['event_id'], 'ticket.id' => $this->data['ticket']['sections']);		
		$fields = array('ticket.name', 'ticket.price');
		$section = $this->Ticket->find('first', array('fields' => $fields, 'recursive' => 0, 'conditions' => $conditions));
		$this->set('section', $section);
		
		//Find Available Seats
		$seat_conditions = array('seat.event_id' => $post['ticket']['event_id'], 'seat.ticket_id' => $post['ticket']['sections'], 'status' => '');
		$seats = $this->Seat->find('all', array('conditions' => $seat_conditions, 'order' => array('seat.row ASC', 'seat.number ASC'), 'limit' => $post['ticket']['quantity']));
		
		$num_seats = count($seats);
		
		//Redirect if tickets are unavailable
		if ($num_seats < $post['ticket']['quantity']) {
			$this->Session->setFlash('We were unable to find '.$post['ticket']['quantity'].' seats in the section. Please try a smaller number or a different section.');
			$this->redirect('/events/view/'.$post['ticket']['event_id']);
			}
		
		//Build List of seats with row numbers
		$s = array();
		$seat_ids = array();
		foreach ($seats as $seat) {
			$s[] = $seat['seat']['row']."-".$seat['seat']['number'];
			$seat_ids[] = $seat['seat']['id'];
			}

		//Calculate ticket subtotal
		$subtotal = $section['ticket']['price'] * $post['ticket']['quantity'];

		
		$this->set('seats', $seats);
		$this->set('seat_ids', $seat_ids);
		$this->set('s', $s);
		$this->set('subtotal', $subtotal);
		
		//Save info as new order
		$this->Order->create();
		$order = array('event_id' => $post['ticket']['event_id'], 'seat_ids' =>  implode(":", $seat_ids), 'subtotal' => $subtotal, 'seats' => implode(', ', $s));
		$this->Order->save($order);
		
		$this->set('order_id', $this->Order->id);

		}

	function delivery() {
		//Get current order info
		$order = $this->Order->find($this->data['order']['order_id']);
		$this->set('event', $order['event']);
		$this->set('order_id', $this->data['order']['order_id']);
		}
	
	function set_delivery() {
		$order_id = $this->data['Order']['id'];
		if (!$order_id) {
			$this->Session->setFlash('Unable to confirm tickets. Please try again.');
			$this->redirect('/events');
			}
		else {
			$this->Order->id = $order_id;
			$this->Order->save($this->data);
			$order = $this->Order->findById($order_id);
			$this->set('order', $order);
			$this->Session->write('Order.id', $order_id);
			$this->redirect('/tickets/user');
			}
		}
	
	function user() {
		}
	
	function login() {
		if(!$this->Auth->login($this->data)) {
			$this->Session->setFlash('The username and password did not match.', 'default', null, 'login');
			$this->redirect('/tickets/user');
			}
		else {
			$this->redirect('/tickets/checkout');
			}
		}
	
    function register() {
	    if (!empty($this->data)) {
    	    if ($this->data['User']['password'] == $this->Auth->password($this->data['User']['psword'])) {
        	    $this->User->create();
            	if(!empty($this->data['User']['username']) && !empty($this->data['User']['password']) && $this->User->save($this->data)) {
					$this->Auth->login($this->data);
					$this->redirect('/tickets/checkout');
            		}
            	else {
					$this->Session->setFlash('Please enter all information to register.', 'default', null, 'register');
					$this->redirect('/tickets/user');	
            		}
				}
			else {
				$this->Session->setFlash('The Passwords you entered did not match.', 'default', null, 'register');
				$this->redirect('/tickets/user');
				}
			}
		else {
			$this->Session->setFlash('Please enter information to register.', 'default', null, 'register');
			$this->redirect('/tickets/user');
			}
		}
		
	function checkout() {
		//Get Order.id from session
		$order_id = $this->Session->read('Order.id');
		$order = $this->Order->findById($order_id);
		$this->set('order', $order);
		$this->set('event', $order['event']);
		//$this->Session->delete('Order.id');
		}
}
?>